<template class="publish-container">
  <div>
    <!-- 整个页面复制发表文章的页面,简单做一些修改 -->
    <el-card class="box-card">
      <!-- 面包屑头部 -->
      <div slot="header" class="clearfix">
        <my-breadcrumb>编辑文章</my-breadcrumb>
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <!-- 富文本编辑器的引用 -->
        <el-form-item label="内容：">
          <quill-editor
            class="ql-editor"
            v-model="form.content"
            :options="editorOption"
          ></quill-editor>
        </el-form-item>

        <el-form-item label="封面">
          <el-radio-group v-model="form.coverType" @change="hImageTypeChange">
            <el-radio :label="1" name="type">单图</el-radio>
            <el-radio :label="3" name="type">三图</el-radio>
            <el-radio :label="0" name="type">无图</el-radio>
            <el-radio :label="-1" name="type">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 图片显示区域 -->
        <el-form-item label="" v-if="form.coverType > 0">
          <el-row>
            <el-col :span="6" v-for="(item, idx) in form.coverType" :key="idx">
              {{ idx }}
              <my-cover v-model="form.coverImages[idx]" />
            </el-col>
          </el-row>
        </el-form-item>

        <!-- 频道组件 -->
        <my-channel v-model="form.channelId"></my-channel>

        <el-form-item>
          <el-button :loading="isLoading" type="primary" @click="hSave">保存修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入富文本编辑器的样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入它的组件
import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'addArticle',
  components: {
    quillEditor
  },
  data () {
    return {
      // 给几个需要有加载动作的按钮添加加载事件
      isLoading: false,
      // 用v-model,那么对应的属性值需要绑定的内容,data的form中要有
      form: {
        title: '好文章-当我第一次看见你的时候',
        content: '你好hello',
        coverType: '',
        coverImages: [], // 保存对应的封面图的地址
        channelId: ''
      },
      // 富文本的配置对象,文本要求的
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }
    }
  },
  created () {
    this.loadArticle(this.$route.params.id)
  },
  methods: {
    // 跳转到编辑页面的第一件事情就是拿去目标文章的内容
    async loadArticle (id) {
      try {
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles/' + id
        })
        console.log(res)
        const { title, channel_id: channelId, content, cover } = res.data.data
        this.form.id = id
        this.form.title = title
        this.form.channelId = channelId
        this.form.content = content
        this.form.coverType = cover.type
        this.form.coverImages = cover.images
      } catch (err) {
        console.log(err)
      }
    },
    // 构建参数
    creatParams () {
      const { title, content, channelId, coverType, coverImages } = this.form
      const paramsObj = {
        title: title,
        content: content,
        cover: {
          type: coverType,
          images: coverImages
        },
        channel_id: channelId
      }
      return paramsObj
    },
    // 发送请求保存修改
    dSave (state) {
      // console.log(state)
      const paramsObj = this.creatParams()
      this.$axios({
        method: 'PUT',
        url: '/mp/v1_0/articles/' + this.form.id,
        data: paramsObj
      })
        .then((res) => {
          this.$message.success('保存修改文章成功')
          // console.log(res)
        })
        .catch((err) => {
          console.log(err)
          this.$message.error('保存修改文章失败')
        })
    },
    // 点击触发请求事件保存修改
    hSave () {
      // .validate()是对整个表单进行校验的方法,参数为一个回调函数。
      // 该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段。
      // data内部没有填写验证规则,所以不需要验证
      this.$refs.form.validate(valid => {
        if (valid) {
          this.dSave()
        }
      })
    },
    // 用户点击切换图片的数量显示
    hImageTypeChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType <= 0) {
        this.form.coverImages.length = 0
      }
    }
  }
}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能

.ql-editor {
  height: 300px;
}

// .publish-container /deep/ .ql-editor{
//   height: 300px;
// }

// .publish-container /deep/ .ql-toolbar.ql-snow{
//   padding: 0 8px;
// }
</style>
